package net.example.model;


import com.oraycn.esbasic.helpers.StringHelper;
import com.oraycn.omcs.utils.SerializeUtils;
import java.io.IOException;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

/**
 * Created by ZN on 2015/12/5.
 */
public class GGGroup {
    private String Announce;
    private String CommentName;
    private Date CreateTime;
    private String CreatorID;
    private String GroupID;
    private int GroupType;
    private LastWordsRecord lastWordsRecord;
    private String Members;
    private String Name;
    private int Version;

    public GGGroup(){}

    public GGGroup(String creatorID, GroupType groupType , String groupID, String groupName, String announce, List<String> members, boolean isPrivate)
    {
        this.CreatorID=creatorID;
        this.GroupType= groupType.ordinal();
        this.GroupID=groupID;
        this.Name=groupName;
        this.Announce=announce;
        this.Members= StringHelper.ContactString(members,",");
        this.CreateTime=new Date();
        this.CommentName="";
        this.IsPrivate=isPrivate;
    }
    public int getChildCount() {
        int count = 0;
        try {
            String[] memeberArray = Members.split(",");
            count = memeberArray.length;
        } catch (Exception ex) {
        }
        return count;
    }

    public List<String> getChildMemberList(){
        List<String> list=new ArrayList<String>();
        try {
            String[] memeberArray = Members.split(",");
            list= new ArrayList<>(Arrays.asList(memeberArray)) ;//解决Arrays.asList() 调用remove，Add会java.lang.UnsupportedOperationException异常
        } catch (Exception ex) {
        }
        return list;
    }

    public static ArrayList<GGGroup> deserializeGroupList(byte[] bytes) throws IOException {
        ByteBuf buffer = Unpooled.wrappedBuffer(bytes).order(ByteOrder.LITTLE_ENDIAN);
        ArrayList<GGGroup> list = new ArrayList<GGGroup>();
        int userCount = buffer.readInt();

        for (int i = 0; i < userCount; i++) {
            GGGroup user = new GGGroup();
            user.deserialize(buffer);
            list.add(user);
        }
        return list;
    }

    public String getAnnounce() {
        return Announce;
    }

    public void setAnnounce(String announce) {
        Announce = announce;
    }

    public String getCommentName() {
        return CommentName;
    }

    public void setCommentName(String commentName) {
        CommentName = commentName;
    }

    public Date getCreateTime() {
        return CreateTime;
    }

    public void setCreateTime(Date createTime) {
        CreateTime = createTime;
    }

    public String getCreatorID() {
        return CreatorID;
    }


    public void setCreatorID(String creatorID) {
        CreatorID = creatorID;
    }

    public String getGroupID() {
        return GroupID;
    }

    public void setGroupID(String groupID) {
        GroupID = groupID;
    }

    public int getGroupType() {
        return GroupType;
    }

    public void setGroupType(int groupType) {
        GroupType = groupType;
    }

    public LastWordsRecord getLastWordsRecord() {
        return lastWordsRecord;
    }

    public void setLastWordsRecord(LastWordsRecord lastWordsRecord) {
        this.lastWordsRecord = lastWordsRecord;
    }

    public String getMembers() {
        return Members;
    }

    public void addMembers(List<String> memberIDs)
    {
        List<String> list= this.getChildMemberList();
        for (String memberID:memberIDs) {
            if(!list.contains(memberID))
            {
                this.Members+=","+memberID;
            }
        }
    }

    public void removeMembers(List<String> memberIDs)
    {
        List<String> list= this.getChildMemberList();
        for (String memberID:memberIDs) {
            list.remove(memberID);
        }
        this.Members=StringHelper.ContactString(list,",");
    }

    public void setMembers(String members) {
        Members = members;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public int getVersion() {
        return Version;
    }

    public void setVersion(int version) {
        Version = version;
    }

    public boolean IsPrivate=false;

    public void deserialize(byte[] bytes) throws IOException {
        ByteBuf buffer = Unpooled.wrappedBuffer(bytes).order(ByteOrder.LITTLE_ENDIAN);
        buffer.readInt();
        this.Announce = SerializeUtils.readStrIntLen(buffer);
        this.CommentName = SerializeUtils.readStrIntLen(buffer);
        this.CreateTime = new Date(buffer.readLong());
        this.CreatorID = SerializeUtils.readStrIntLen(buffer);
        this.GroupID = SerializeUtils.readStrIntLen(buffer);
        this.GroupType = buffer.readInt();
        this.IsPrivate=buffer.readByte() == (byte) 1;
        this.lastWordsRecord = new LastWordsRecord();
        this.lastWordsRecord.deserialize(buffer);
        this.Members = SerializeUtils.readStrIntLen(buffer);
        this.Name = SerializeUtils.readStrIntLen(buffer);
        this.Version = buffer.readInt();
    }

    private void deserialize(ByteBuf buffer) throws IOException {
        buffer.readInt();
        this.Announce = SerializeUtils.readStrIntLen(buffer);
        this.CommentName = SerializeUtils.readStrIntLen(buffer);
        this.CreateTime = new Date(buffer.readLong());
        this.CreatorID = SerializeUtils.readStrIntLen(buffer);
        this.GroupID = SerializeUtils.readStrIntLen(buffer);
        this.GroupType = buffer.readInt();
        this.IsPrivate=buffer.readByte() == (byte) 1;
        this.lastWordsRecord = new LastWordsRecord();
        this.lastWordsRecord.deserialize(buffer);
        this.Members = SerializeUtils.readStrIntLen(buffer);
        this.Name = SerializeUtils.readStrIntLen(buffer);
        this.Version = buffer.readInt();
    }
}
